import Mock from 'mockjs'

// 生成银行卡列表
const generateBankCards = (userId, count = 3) => {
    const list = []
    for (let i = 0; i < count; i++) {
        list.push({
            id: i + 1,
            userId,
            cardNo: `622202${Mock.Random.string('number', 10)}`,
            bankName: Mock.Random.pick(['中国工商银行', '中国建设银行', '中国农业银行', '中国银行', '交通银行']),
            bankCode: Mock.Random.pick(['ICBC', 'CCB', 'ABC', 'BOC', 'COMM']),
            holderName: Mock.Random.cname(),
            phone: `138${Mock.Random.string('number', 8)}`,
            cardType: Mock.Random.integer(0, 1),
            isDefault: i === 0 ? 1 : 0,
            status: 1,
            createTime: Mock.Random.datetime(),
            updateTime: Mock.Random.datetime()
        })
    }
    return list
}

// 模拟数据
export default {
    // 获取用户银行卡列表接口
    '/api/bank-cards/user/\\d+ get': (options) => {
        const userId = parseInt(options.url.match(/\/api\/bank-cards\/user\/(\d+)/)[1])
        return {
            code: 200,
            msg: '获取成功',
            data: generateBankCards(userId)
        }
    },

    // 获取银行卡详情接口
    '/api/bank-cards/\\d+ get': (options) => {
        const id = parseInt(options.url.match(/\/api\/bank-cards\/(\d+)/)[1])
        return {
            code: 200,
            msg: '获取成功',
            data: {
                id,
                userId: 1,
                cardNo: `622202${Mock.Random.string('number', 10)}`,
                bankName: Mock.Random.pick(['中国工商银行', '中国建设银行', '中国农业银行', '中国银行', '交通银行']),
                bankCode: Mock.Random.pick(['ICBC', 'CCB', 'ABC', 'BOC', 'COMM']),
                holderName: Mock.Random.cname(),
                phone: `138${Mock.Random.string('number', 8)}`,
                cardType: Mock.Random.integer(0, 1),
                isDefault: Mock.Random.integer(0, 1),
                status: 1,
                createTime: Mock.Random.datetime(),
                updateTime: Mock.Random.datetime()
            }
        }
    },

    // 添加银行卡接口
    '/api/bank-cards post': (options) => {
        const data = JSON.parse(options.body)
        return {
            code: 200,
            msg: '添加成功',
            data: {
                id: Mock.Random.integer(1, 100),
                ...data,
                status: 1,
                createTime: Mock.Random.datetime(),
                updateTime: Mock.Random.datetime()
            }
        }
    },

    // 更新银行卡接口
    '/api/bank-cards put': () => {
        return {
            code: 200,
            msg: '更新成功',
            data: null
        }
    },

    // 删除银行卡接口
    '/api/bank-cards/\\d+ delete': () => {
        return {
            code: 200,
            msg: '删除成功',
            data: null
        }
    },

    // 设置默认银行卡接口
    '/api/bank-cards/\\d+/default put': () => {
        return {
            code: 200,
            msg: '设置成功',
            data: null
        }
    }
}